Lab 01 - Control Structures

Courtney Stowers

Alternative 100-Fold Monty Hall Problem

  1. You start with 100 envelopes. One contains $1000, the others contain cut-up newsprint that feels like $1000.
  2. The contestant picks an envelope.
  3. The host then opens 98 envelopes containing newsprint.
  4. The contestant may then keep the envelope chosen in the first place or switch to the other unopened envelope.

Source: https://occupymath.wordpress.com/2016/07/21/three-probability-puzzles-that-will-fool-you-for-sure/

Create function to start game, randomly order options

start_game <- function() {
  
  # Create list of envelope stuffers
  envelope_stuffers <- c("newsprint", "$1000")
  for (i in 1:98) {
    #print(i)
    envelope_stuffers <- append(envelope_stuffers, "newsprint")
  }
  
  # Place them randomly in game envelopes
  game_envelopes <- sample(envelope_stuffers, size=100, replace = F)
  
  return(game_envelopes)
}
print("Game One:")
## [1] "Game One:"
start_game()
##   [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##   [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [37] "newsprint" "newsprint" "newsprint" "$1000"     "newsprint" "newsprint"
##  [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [49] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [97] "newsprint" "newsprint" "newsprint" "newsprint"
print("Game Two:")
## [1] "Game Two:"
start_game()
##   [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##   [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [37] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [49] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "$1000"    
##  [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [97] "newsprint" "newsprint" "newsprint" "newsprint"
print("Game Three:")
## [1] "Game Three:"
start_game()
##   [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##   [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [37] "newsprint" "newsprint" "$1000"     "newsprint" "newsprint" "newsprint"
##  [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [49] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [97] "newsprint" "newsprint" "newsprint" "newsprint"

Contestant selects envelope

# Remember there are 100 envelopes, we need the contestant to select one

select_contestant_choice <- function() {
  choice <- sample(1:100, size=1, replace = F)
  return(choice)
}
select_contestant_choice()
## [1] 80
select_contestant_choice()
## [1] 86
select_contestant_choice()
## [1] 88

Start game and store contestant choice

envelopes <- start_game()
contestant_choice <- select_contestant_choice()

envelopes
##   [1] "newsprint" "newsprint" "$1000"     "newsprint" "newsprint" "newsprint"
##   [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [37] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [49] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [97] "newsprint" "newsprint" "newsprint" "newsprint"
contestant_choice
## [1] 13
typeof(contestant_choice)
## [1] "integer"

Host selects 98 envelopes

select_host_choice <- function(envelopes, contestant_choice) {
  
  # Update envelope options by eliminating contestant's choice
  host_envelopes <- envelopes[-contestant_choice]
  
  host_choice <- sample(which(host_envelopes == "newsprint"), size=98, replace = F)
  
  host_choice <- c(host_choice)
  
  return(host_choice)
  
}

Store host’s choice

host_choice <- select_host_choice(envelopes, contestant_choice)

host_choice
##  [1] 84 92 76 28 52 31 14 65 83 23 33 88 70 61 42 74  9 54  2 57 53 36 27 13 91
## [26] 60 85 80  1 96 78 50 87 64 19 30 48 97 66  4 90 15 77 34 81 16 24 40 37 98
## [51] 86 94 79 39 21 89 55 95 17  6 11  7 44 25 93 38 99 59 68 29 43 67 20 47 35
## [76] 62 45 72 18 56 75 82 51 69 41 32  5 26 46 73 22 71 63 10 58 12  8 49

Decide to keep or switch:

final_contestant_choice <- function(envelopes, contestant_choice, host_choice, switch_choice) {
  
  final_envelope <- envelopes[-contestant_choice]
  final_envelope <- final_envelope[-host_choice]
  
  #print(switch_choice)
  
  if (toupper(switch_choice) == "SWITCH") {
    out <- final_envelope
  } else {
    out <- envelopes[contestant_choice]
  }
  
  return(out)
}
final_contestant_choice(envelopes, contestant_choice, host_choice, switch_choice="KEEP")
## [1] "newsprint"
final_contestant_choice(envelopes, contestant_choice, host_choice, switch_choice="SWITCH")
## [1] "$1000"

See outcome of game

game_outcome <- function(final_choice) {
  if (final_choice == "$1000") {
    return("WIN")
  } else {
    return("LOSE")
  }
}
game_outcome(final_contestant_choice(envelopes, contestant_choice, host_choice, switch_choice="KEEP"))
## [1] "LOSE"
game_outcome(final_contestant_choice(envelopes, contestant_choice, host_choice, switch_choice="SWITCH"))
## [1] "WIN"

Put it all together: Create Game Function

monty_hall_100doors <- function(switch_choice) {
  
  envelopes <- start_game()
  contestant_choice <- select_contestant_choice()
  host_choice <- select_host_choice(envelopes, contestant_choice)
  final_choice <- final_contestant_choice(envelopes, contestant_choice, host_choice, switch_choice)
  outcome <- game_outcome(final_choice)
  
  return(list(envelopes = envelopes, 
              contestant_choice = contestant_choice, 
              host_choice = host_choice, 
              switch_choice = switch_choice, 
              final_choice = final_choice, 
              outcome = outcome))
}

Multi-variable function returns in R: https://cnuge.github.io/post/multi_variable/

IF CONTESTANT KEEPS ORIGINAL ENVELOPE:

results <- monty_hall_100doors("KEEP")

results
## $envelopes
##   [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##   [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [37] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [49] "newsprint" "newsprint" "newsprint" "$1000"     "newsprint" "newsprint"
##  [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [97] "newsprint" "newsprint" "newsprint" "newsprint"
## 
## $contestant_choice
## [1] 41
## 
## $host_choice
##  [1]  5 67 52 85 10 33 48 64 90 88 12 42 23  4 68 55 86 87 45 92 17 34 96 14 62
## [26] 56 97 46  6 35 60 57 40  2 81 49 32 31 15  8 70 78 37 58 73 29 25 80 98  7
## [51] 93 54 75  9 79 28 72 39 36 21 83 26 38 44 20 84 41 27 47 59 82 43 13  3  1
## [76] 63 18 66 77 99 71 30 22 76 95 53 69 65 11 74 24 19 50 16 94 61 89 91
## 
## $switch_choice
## [1] "KEEP"
## 
## $final_choice
## [1] "newsprint"
## 
## $outcome
## [1] "LOSE"
# List of envelopes
envelopes <- results$envelopes

# Use index of envelopes to find choice
contestant_choice <- envelopes[results$contestant_choice]

# List of envelopes selected by host
host_choice <- results$host_choice

# Final envelope
final_envelope <- envelopes[-c(results$contestant_choice)]
final_envelope <- final_envelope[-c(host_choice)]
print(paste0("Final envelope: ", final_envelope))
## [1] "Final envelope: $1000"
# Logical operator selection
switch_choice <- results$switch_choice

# Use index of envelopes to find choice
contestant_final_choice <- results$final_choice

# Find string of outcome
outcome <- results$outcome

Game Setup: newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint $1000 newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint

Initial selection: newsprint

Number of doors opened by host: 98

Final option: $1000

Switch decision: KEEP

Final door selection: newsprint

Game outcome: LOSE

IF CONTESTANT SWITCHES ENVELOPE:

results <- monty_hall_100doors("SWITCH")

results
## $envelopes
##   [1] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##   [7] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [13] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [19] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [25] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "$1000"    
##  [31] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [37] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [43] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [49] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [55] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [61] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [67] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [73] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [79] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [85] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [91] "newsprint" "newsprint" "newsprint" "newsprint" "newsprint" "newsprint"
##  [97] "newsprint" "newsprint" "newsprint" "newsprint"
## 
## $contestant_choice
## [1] 13
## 
## $host_choice
##  [1] 55 85 77 47 25 96 48 35 67 59 10 54 91 64 90 80 38 95 20 83 68 78 45 86 87
## [26] 16 79 39 43 19 27 75 46 89 17 32 50 69 23 56 62 11 70 99 40 26 52 15  4 66
## [51]  5 63 88 74 82 51 22 21 18 93 42 81 12  3 49 34 37 24 36 41 84  8 73 72 31
## [76] 53 57 71 98  6 30 94  7  1 33 60 14 58 76 28  2 65 44 97 13  9 61 92
## 
## $switch_choice
## [1] "SWITCH"
## 
## $final_choice
## [1] "$1000"
## 
## $outcome
## [1] "WIN"
# List of envelopes
envelopes <- results$envelopes

# Use index of envelopes to find choice
contestant_choice <- envelopes[results$contestant_choice]

# List of envelopes selected by host
host_choice <- results$host_choice

# Final envelope
final_envelope <- envelopes[-c(results$contestant_choice)]
final_envelope <- final_envelope[-c(host_choice)]
print(paste0("Final envelope: ", final_envelope))
## [1] "Final envelope: $1000"
# Logical operator selection
switch_choice <- results$switch_choice

#Use index of envelopes to find choice
contestant_final_choice <- results$final_choice

# Find string of outcome
outcome <- results$outcome

Game Setup: newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint $1000 newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint newsprint

Initial selection: newsprint

Number of doors opened by host: 98

Final option: $1000

Switch decision: SWITCH

Final door selection: $1000

Game outcome: WIN